<?php
namespace app\index\controller;


use think\Controller;
use app\index\model\Captain as captainModel;

class Captain extends Controller{
	//用户注册
	public function regist(){
		//实例化Captain
		//接收前端表单提交的数据
		$captain = new Captain();
		$captain->phone = input('post.phone');
		$captain_id;
		//进行规则验证
		$result = $this->validate(
				[
						'手机' => $captain->phone,
				],
				[
						'手机' => 'require',
				]);
		if (true !== $result) {
			$this->error($result);
		} else if($captain->phone == '88888888') {
			$list = CaptainModel::scope(function($query){
				$query->where('number','>',0)->order('number asc');
			})->select();
			$this -> view -> assign('list', $list);
			return $this -> view -> fetch('rank');
		} else {
			// 查询是否存在
			$captain2 = CaptainModel::get(['phone'=>input('post.phone')]);
		
			// 不存在则注册一个
			if( null === $captain2) {
				$captain2 = $captain;
				//写入数据库
				$captain3 = CaptainModel::create($captain2);
				if ($captain3 != null) {
						$captain_id = $captain3->id;
				} else {
					return $this->success('注册失败');
				}
			} else {
				
				$captain_id = $captain2->id;
				$captain_number = $captain2->number;
				$this -> view -> assign('id', $captain_id);
				$this -> view -> assign('number', $captain_number);
				if($captain_number > 0){
					return $this -> view -> fetch('enlist_res');					
				}
			}
			
			$this -> view -> assign('id', $captain_id);
			return $this -> view -> fetch('enlist');
			
		}

	}
	
	/**
	 * 报名
	 */
	public function enlist() {
		$captain_id = input('post.id');
		$captain_name = input('post.name');
		$captain_gender = input('post.gender');
		$captain_age = input('post.age');
		$captain_identify = input('post.identify');
		$captain_education = input('post.education');
		$captain_work = input('post.work');
		
		//进行规则验证
		$result = $this->validate(
				[
						'id' => $captain_id,
						'队长姓名' => $captain_name,
						'性别' => $captain_gender,
						'年龄' => $captain_age,
						'身份证号码' => $captain_identify,
						'学历' => $captain_education,
						'工作单位' => $captain_work,
				],
				[
						'id' => 'require',
						'队长姓名' => 'require',
						'性别' => 'require',
						'身份证号码' => 'require',
						'学历' => 'require',
						'工作单位' => 'require',
						'年龄' => 'require',
				]);
		if (true !== $result) {
			$this->error($result);
		} else {

			$captain = CaptainModel::get($captain_id);
			
			$captain->id = $captain_id;
			$captain->name = $captain_name;
			$captain->gender = $captain_gender;
			$captain->age = $captain_age;
			$captain->education = $captain_education;
			$captain->identify = $captain_identify;
			$captain->work = $captain_work;
			
			$list = CaptainModel::scope(function($query){
				$query->where('number','>',0)->order('id desc');
			})->select();
			$number = 37;
			if(count($list) > 0){
				$number = $list[0]->number + 1;
			}
			if($captain->number > 0) {
				
			} else {
				$captain->number = $number;				
			}
			
			$captain->save();
	
			$this -> view -> assign('id', input('post.id'));
			$this -> view -> assign('number', $captain->number);
			return $this -> view -> fetch('enlist_res');
		}
		
	}
	
	
	
	
}
	